package lk.icta.common.dao.impl;

import lk.icta.common.AppConstants;
import lk.icta.common.ErrorCodes;
import lk.icta.common.dao.RailwayTrackDAO;
import lk.icta.common.entity.RailwayTrack;
import lk.icta.user.entity.User;

import javax.persistence.Query;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Supun Viraj Rathnayaka
 */
public class RailwayTrackDAOImpl extends BaseDAO implements RailwayTrackDAO {

    private final Map<Integer, String> siNames = new HashMap<Integer, String>();
    private final Map<Integer, String> enNames = new HashMap<Integer, String>();
    private final Map<Integer, String> taNames = new HashMap<Integer, String>();

    @Override
    public Map<Integer, String> getAllRailwayTracksByUserLanguage(String language, User user) {
        logger.debug("Listing railway tracks in {} by {}", language, user.getUsername());
        Query q = em.createNamedQuery("getAllRailwayTracksByUserLanguage");
        List<RailwayTrack> railwayTrackList = q.getResultList();
        for (RailwayTrack railwayTrack : railwayTrackList) {
            siNames.put(railwayTrack.getId(), railwayTrack.getSiRailwayTrackName());
            enNames.put(railwayTrack.getId(), railwayTrack.getEnRailwayTrackName());
            taNames.put(railwayTrack.getId(), railwayTrack.getTaRailwayTrackName());
        }
        if (AppConstants.SINHALA.equals(language)) {
            return siNames;
        } else if (AppConstants.ENGLISH.equals(language)) {
            return enNames;
        } else if (AppConstants.TAMIL.equals(language)) {
            return taNames;
        } else {
            handleException("Unsupported language : " + language, ErrorCodes.INVALID_LANGUAGE);
        }
        return Collections.emptyMap();
    }

    public RailwayTrack getRailwayTrackById(int RailwayTrack){
        logger.debug("Loading Track {}", RailwayTrack);
        return em.find(RailwayTrack.class, RailwayTrack);
    }
}
